﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using FRMS_N71SP.BUS;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace FRMS.TimedTask
{
    public class N71SPSyncRecord : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                //定时删除日志
                DeleteLog();
                N71SPDeviceBUS n71SPDeviceBUS = new N71SPDeviceBUS();
                string starttime = DateTime.Now.AddDays(-1).ToString();
                string endtime = DateTime.Now.ToString();
                n71SPDeviceBUS.SyncRecord(starttime, endtime);
            }
            catch (Exception ex)
            {
                LogHelper.Log("同步考勤记录异常" + ex.ToString());
            }
        }

        public void DeleteLog()
        {
            try
            {
                int day = Convert.ToInt32(AppSettingsHelper.Configuration["LogSave"]) * -1;
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
                if (!Directory.Exists(logPath))
                {
                    return;
                }
                DirectoryInfo folder_base = new DirectoryInfo(logPath);
                FileInfo[] files_base = folder_base.GetFiles("*.txt");  //获取.txt文件
                foreach (var file_txt in files_base)
                {
                    //获取文件创建时间
                    DateTime fileCreateTime = file_txt.CreationTime;
                    //获取当前时间
                    DateTime now = DateTime.Now.Date.AddDays(day);
                    if (fileCreateTime < now)
                    {
                        try
                        {
                            File.Delete(file_txt.FullName);
                            LogHelper.Log("删除日志" + file_txt.FullName);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Log("定时删除日志异常"+e.ToString());
            }
        }
    }
}
